package ltd.newbee.mall.service.impl;

import ltd.newbee.mall.common.Constants;
import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.dao.AdminUserDAO;
import ltd.newbee.mall.dao.AdminUserMapper;
import ltd.newbee.mall.entity.AdminUser;
import ltd.newbee.mall.service.AdminUserService;
import ltd.newbee.mall.util.MD5Util;
import ltd.newbee.mall.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Resource
    private AdminUserMapper adminUserMapper;

    @Autowired
    private AdminUserDAO adminUserDAO;

    @Override
    public AdminUser login(String userName, String password) {
        String passwordMd5 = MD5Util.MD5Encode(password, Constants.UTF_ENCODING);
        return adminUserMapper.login(userName, passwordMd5);
    }

    @Override
    public AdminUser getUserDetailById(Integer loginUserId) {
        return adminUserMapper.selectByPrimaryKey(loginUserId);
    }

    @Override
    public Boolean updatePassword(Integer loginUserId, String originalPassword, String newPassword) {
        AdminUser adminUser = adminUserMapper.selectByPrimaryKey(loginUserId);
        //当前用户非空才可以进行更改
        if (adminUser != null) {
            String originalPasswordMd5 = MD5Util.MD5Encode(originalPassword, Constants.UTF_ENCODING);
            String newPasswordMd5 = MD5Util.MD5Encode(newPassword, Constants.UTF_ENCODING);
            //比较原密码是否正确
            if (originalPasswordMd5.equals(adminUser.getLoginPassword())) {
                //设置新密码并修改
                adminUser.setLoginPassword(newPasswordMd5);
                if (adminUserMapper.updateByPrimaryKeySelective(adminUser) > 0) {
                    //修改成功则返回true
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public Boolean updateName(Integer loginUserId, String loginUserName, String nickName) {
        AdminUser adminUser = adminUserMapper.selectByPrimaryKey(loginUserId);
        //当前用户非空才可以进行更改
        if (adminUser != null) {
            //设置新名称并修改
            adminUser.setLoginUserName(loginUserName);
            adminUser.setNickName(nickName);
            if (adminUserMapper.updateByPrimaryKeySelective(adminUser) > 0) {
                //修改成功则返回true
                return true;
            }
        }
        return false;
    }

    @Override
    public PageResult getAdminPage(PageRequest pageRequest) {
        Page<AdminUser> adminUserPageList = adminUserDAO.findAll(pageRequest);
        List<AdminUser> adminUserInfos = adminUserPageList.getContent();

        int total = (int)adminUserDAO.count();

        PageResult pageResult = new PageResult(adminUserInfos, total, pageRequest.getPageSize(), pageRequest.getPageNumber() + 1);

        return  pageResult;
    }

    @Override
    public String save(AdminUser adminUser) {
        try
        {
            adminUserDAO.save(adminUser);
            return ServiceResultEnum.SUCCESS.getResult();
        }catch (Exception e)
        {
            return ServiceResultEnum.DB_ERROR.getResult();
        }
    }

    @Override
    public String modify(AdminUser adminUser) {
        if (adminUserDAO.findById(adminUser.getAdminUserId()).isPresent()) {
            adminUserDAO.save(adminUser);
            return ServiceResultEnum.SUCCESS.getResult();
        } else {
            //抛出自定义异常供货商不存在
            return ServiceResultEnum.SUPPLIER_NOT_EXIST_ERROR.getResult();
        }
    }

    @Override
    public String deleteById(Integer loginUserId) {
        try
        {
            adminUserDAO.deleteById(loginUserId);
            return ServiceResultEnum.SUCCESS.getResult();
        }catch (Exception e)
        {
            return ServiceResultEnum.DB_ERROR.getResult();
        }
    }
}
